package com.service.totalauth.controller;

import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.totalauth.dto.AddProgrammeInfoExamineDto;
import com.service.totalauth.dto.ApplicationProgrammeInfoDto;
import com.service.totalauth.dto.DeleteProgrammeInfoExamineDto;
import com.service.totalauth.dto.ScreenUserDto;
import com.service.totalauth.entity.PortalUser;
import com.service.totalauth.service.ManageProgrammeInfoService;
import com.service.totalauth.service.PortalUserService;
import com.service.totalauth.vo.BindAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 方案信息表(ManageProgrammeInfo)表控制层
 *
 * @author makejava
 * @since 2021-12-03 09:38:49
 */
@RestController
@Api(value = "方案信息表", tags = "方案信息表")
@RequestMapping("manageProgrammeInfo")
public class ManageProgrammeInfoController {
    /**
     * 服务对象
     */
    @Resource
    private ManageProgrammeInfoService manageProgrammeInfoService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private PortalUserService portalUserService;
    /**
     * 查询用户所拥有的方案,
     * @return
     */
    @ApiOperation(value = "查询租户所拥有的方案")
    @GetMapping(value = "/queryManageProgrammeInfo")
    public Result queryManageProgrammeInfo(@RequestHeader(value = "cloud-Auth-Token",required = false)
                                           @ApiParam("token")String cloudAuthToken,
                                           @RequestParam(value = "programmeInfoId",required = false) @ApiParam("方案id") String programmeInfoId,
                                           @RequestParam(value = "type",required = false) @ApiParam("0--普通 1--飞书 2--钉钉 3--app 4--微信小程序和企业微信专用  8--微信小程序分享至企业微信") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                PortalUser portalUser = new PortalUser();
                portalUser.setManageId(userId);
                List<ScreenUserDto> screenUserDtos = portalUserService.queryAll(userId);
                if (screenUserDtos == null || screenUserDtos.size() == 0 ){
                    return Result.failure(10013,"您的账号还未关联华为云管理账号，请进入后台进行关联");
                }
                if (programmeInfoId != null && !programmeInfoId.equals("") ) { // 方案id存在查询单个
                    return manageProgrammeInfoService.queryManageProgrammeInfo(userId,programmeInfoId,type);
                }
                return manageProgrammeInfoService.queryManageProgrammeInfo(userId,"",type);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }

    /**
     * portal页面查询,
     * @param programmeInfoBindId
     * @return
     */
    @ApiOperation(value = "0--普通/1--飞书/2--钉钉/3--app的portal页面查询/4--微信小程序和企业微信专用  8--微信小程序分享至企业微信")
    @GetMapping(value = "/queryPortalHtml")
    public Result queryPortalHtml(@RequestParam(value = "programmeInfoBindId") @ApiParam("绑定id") String programmeInfoBindId,
                                  @RequestParam(value = "type") @ApiParam("0--普通 1--飞书 2--钉钉 3--app 4--微信小程序 8--微信小程序分享至企业微信") Integer type) throws Exception {
        return manageProgrammeInfoService.queryPortalHtml(programmeInfoBindId,type);
    }
    /**
     * 用户新增方案
     * @param addProgrammeInfoExamineDto
     * @return
     */
    @ApiOperation(value = "租户新增方案")
    @PostMapping(value = "/addManageProgrammeInfo",produces="application/json;charset=UTF-8")
    public Result addManageProgrammeInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                         @RequestBody AddProgrammeInfoExamineDto addProgrammeInfoExamineDto
    ) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                return manageProgrammeInfoService.addManageProgrammeInfo(userId,addProgrammeInfoExamineDto);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 用户新增方案
     * @param deleteProgrammeInfoExamineDto
     * @return
     */
    @ApiOperation(value = "租户删除方案")
    @PostMapping(value = "deleteProgramme",produces="application/json;charset=UTF-8")
    public Result  deleteProgramme(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestBody DeleteProgrammeInfoExamineDto deleteProgrammeInfoExamineDto){
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                manageProgrammeInfoService.deleteById(deleteProgrammeInfoExamineDto.getId());
                return Result.ok();
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 获取方案绑定的方案和api以及url
     * @param programmeInfoId
     * @return
     */
    @ApiOperation(value = "获取方案绑定的方案和api以及url")
    @GetMapping(value = "/getUrl")
    public Result getUrl(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                         @RequestParam(value = "programmeInfoId") @ApiParam("方案id") String programmeInfoId,
                         @RequestParam(value = "type") @ApiParam("方案类型") Integer type) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return manageProgrammeInfoService.getUrl(userId,programmeInfoId,type);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 获取方案绑定的方案和api以及url
     * @param WechatId
     * @return
     */
    @ApiOperation(value = "企业微信和微信小程序绑定")
    @GetMapping(value = "/bindWeChatAndQy")
    public Result bindWeChatAndQy(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                         @RequestParam(value = "WechatId") @ApiParam("微信信息id") String WechatId,
                         @RequestParam(value = "qyId") @ApiParam("企业微信id") String qyId,
                         @RequestParam(value = "type") @ApiParam("方案类型") Integer type) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return manageProgrammeInfoService.bindWeChatAndQy(WechatId,qyId,type);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 企业绑定方案和api
     * @param bindAccount
     * @return
     */
    @ApiOperation(value = "app/飞书/钉钉/微信小程序绑定方案和api")
    @PostMapping(value = "/bindAccountAndProgramme")
    public Result bindAccountAndProgramme(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                          @RequestBody BindAccount bindAccount) throws Exception{
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return manageProgrammeInfoService.bindAccountAndProgramme(userId,bindAccount);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 方案下发
     * @return
     */
    @ApiOperation(value = "方案下发")
    @PostMapping(value = "/putIssue")
    public Result putIssue(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                           @RequestBody ApplicationProgrammeInfoDto applicationProgrammeInfoDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return manageProgrammeInfoService.putIssue(applicationProgrammeInfoDto);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
}
